package com.skillworld.webapp.web.pages.rest.game;

import java.util.Locale;

import org.apache.tapestry5.annotations.ContentType;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.services.Request;

import com.skillworld.webapp.model.department.Department;
import com.skillworld.webapp.model.department.rank.DeptRank;
import com.skillworld.webapp.model.gamemap.GameMap;
import com.skillworld.webapp.model.gameservice.GameService;
import com.skillworld.webapp.model.roomdservice.Roomd;
import com.skillworld.webapp.model.roomdservice.RoomdService;
import com.skillworld.webapp.model.user.User;
import com.skillworld.webapp.model.userservice.UserService;
import com.skillworld.webapp.model.util.PermissionDeniedException;
import com.skillworld.webapp.web.services.I18n;
import com.skillworld.webapp.web.util.ErrorMessages;
import com.skillworld.webapp.web.util.UserSession;

import es.udc.pojo.modelutil.exceptions.InstanceNotFoundException;

@ContentType("text/xml")
public class JoinMap
{ 
    
    @SuppressWarnings("unused")
    @Property
    private String errorMessage = null;  
    
    @SuppressWarnings("unused")
    @Property
    private User user;

    @SuppressWarnings("unused")
    @Property
    private Department department;
    
    @SuppressWarnings("unused")
    @Property
    private DeptRank deptRank;
    
    @SuppressWarnings("unused")
    @Property
    private Roomd server;
    
    @SuppressWarnings("unused")
    @Property
    private Locale locale;
    
    @Inject
    private RoomdService roomdService;

    @Inject
    private GameService gameService;
    
    @Inject
    private UserService userService;
    
    @SuppressWarnings("unused")
    @Property
    @InjectService("I18n")
    private I18n i18n;
    
    @SessionState(create=false)
    private UserSession userSession;
    
    private boolean userSessionExists;
    
    @Inject
    private Request request;
   

    void onPassivate() 
    {
        return;
    }
    
    void onActivate()
    {
        if (!userSessionExists)
        {
            errorMessage = ErrorMessages.NOT_LOGGED_IN;
            return;
        }
        
        String mapName = request.getParameter("map");
        String langParam = request.getParameter("lang");
               
        if (mapName == null)
        { 
            errorMessage = ErrorMessages.INVALID_ARGUMENTS;
            return;
        }
  
        try 
        { 
            locale = i18n.selectLocale(langParam);
            long userId = userSession.getUserId();
            GameMap currentMap = gameService.joinMap(userId, mapName);
            user = userService.findUserProfile(userId);
            department = currentMap.getDepartment();
            deptRank = (department != null)
                        ? user.getDepartmentRank(department)
                        : null;
            server = roomdService.findServer(mapName);
        }
        catch (PermissionDeniedException e)
        { errorMessage = ErrorMessages.PERMISSION_DENIED; }
        catch (InstanceNotFoundException e)
        { errorMessage = ErrorMessages.MAP_NOT_FOUND; }

    }
}
